package com.hsy.sunflowerbao.home.ui

import android.graphics.Bitmap
import android.os.Bundle
import android.support.v4.content.res.ResourcesCompat
import com.flux.action.RxStoreChange
import com.flux.utils.GsonUtil
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.ui.activity.BaseActivityWithToolbar
import com.hsy.sunflowerbao.home.store.ScanStore
import com.mylhyl.zxing.scanner.OnScannerCompletionListener
import com.google.zxing.client.result.ParsedResult
import com.google.zxing.BarcodeFormat
import com.mylhyl.zxing.scanner.ScannerOptions
import com.google.zxing.Result
import com.gyf.barlibrary.ImmersionBar
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.home.action.HomeActionCreator
import com.hsy.sunflowerbao.utils.CommonUtils
import kotlinx.android.synthetic.main.activity_scan.*

/**
 * 二维码扫描页面
 * Created on 2018/3/28.
 *
 * @author Yang
 */
class ScanActivity : BaseActivityWithToolbar<ScanStore>(), OnScannerCompletionListener {

    /**
     * 数据缓存器
     */
    override var store = ScanStore(dispatcher)
    /**
     * 事件产生器
     */
    private val creator = HomeActionCreator(dispatcher, manager)
    /**
     * 是否打开闪光
     */
    private var isLighting = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scan)
        setTitle(R.string.txt_home_scan)

        val color = ResourcesCompat.getColor(resources, android.R.color.black, null)
        toolbar.setBackgroundColor(color)

        initScan()
        light.setOnClickListener {
            isLighting = !isLighting
            scanner.toggleLight(isLighting)
        }
    }

    /**
     * 初始化扫描器
     */
    private fun initScan() {
        scanner.setOnScannerCompletionListener(this)
        val builder = ScannerOptions.Builder()
        builder.setFrameSize(224, 224)//扫描框大小，单位：DP
                .setFrameTopMargin(64)//扫描框距顶部距离，单位：DP
                .setFrameCornerHide(false)//显示四角
                .setFrameCornerLength(20)//四角长度
                .setFrameCornerWidth(4)//四角宽度
                .setFrameCornerColor(-0xe66c04)//四角颜色蓝色
                .setLaserLineColor(-0xe66c04)//扫描线颜色蓝色
                .setFrameOutsideColor(0x77000000)//取景框外颜色为半透明黑色
                .setScanMode(BarcodeFormat.QR_CODE)//扫码模式
                .setTipText("将取景框对准二维码，即可自动扫描")//提示文字
                .setTipTextSize(12)//提示字号
                .setTipTextColor(-0x000001)//提示文字颜色白色
        scanner.setScannerOptions(builder.build())
        scanner.restartPreviewAfterDelay(0)
    }

    /**
     * 设置状态栏颜色
     */
    override fun setStatusBar() {
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.black)//设置状态栏颜色
                .statusBarDarkFont(false)//设置状态栏文字颜色
                .init()
    }

    /**
     * 扫描完成监听
     * @param rawResult 扫描结果
     */
    override fun onScannerCompletion(rawResult: Result, parsedResult: ParsedResult, barcode: Bitmap?) {
        CommonUtils.showProgressDialog(this)
        creator.scanUser(rawResult.text)
    }

    override fun onRxStoreChanged(change: RxStoreChange) {
        super.onRxStoreChanged(change)
        if (change.storeId == store.tag) {
            CommonUtils.hideProgressDialog()
//            业务处理
            if (change.success) {
                when (change.type) {
                    ActionType.GET_SCAN_USER -> {
                        startActivity(DetailActivity::class.java, GsonUtil.toJson(store.user))
                        finish()
                    }
                }
            }
        }
    }

    override fun onResume() {
        scanner.onResume()
        super.onResume()
    }

    override fun onPause() {
        scanner.onPause()
        super.onPause()
    }
}